package com.neusoft.neuiotms.common.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.converter.DictionaryTypeConverter;
import com.neusoft.neuiotms.common.dto.DictionaryTypeDTO;
import com.neusoft.neuiotms.common.model.Dictionary;
import com.neusoft.neuiotms.common.model.DictionaryType;
import com.neusoft.neuiotms.common.repository.DictionaryRepository;
import com.neusoft.neuiotms.common.repository.DictionaryTypeRepository;
import com.neusoft.neuiotms.common.service.DictionaryService;
import com.neusoft.neuiotms.common.service.DictionaryTypeService;

@Service
public class DictionaryTypeServiceImpl implements DictionaryTypeService {

    @Autowired
    private DictionaryTypeRepository dictionaryDescRepository;
    @Autowired
    private DictionaryTypeConverter dictionaryDescConverter;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private DictionaryRepository dictionaryRepository;

    @Override
    public List<DictionaryTypeDTO> findAll(final Searchable searchable) {
        final List<DictionaryType> models = this.dictionaryDescRepository.findAll(searchable);
        return this.dictionaryDescConverter.toListDTO(models);
    }

    @Override
    public Page<DictionaryTypeDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<DictionaryType> result = this.dictionaryDescRepository.search(pageable, searchable);
        return this.dictionaryDescConverter.toPageDTO(result);
    }

    @Override
    public DictionaryTypeDTO create(final DictionaryTypeDTO dto) {
        // 校验编码重复性
        final DictionaryType check = this.dictionaryDescRepository.findByType(dto.getType());
        if (check != null) {
            throw new UnitedException("字典类别编码不能重复", new Object[] {});
        }
        final DictionaryType model = this.dictionaryDescConverter.toModel(dto);
        this.dictionaryDescRepository.save(model);
        return this.dictionaryDescConverter.toDTO(model);
    }

    @Override
    public DictionaryTypeDTO update(final Long id, final DictionaryTypeDTO dto) {

        DictionaryType model;

        try {
            model = this.dictionaryDescRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        // 校验编码重复性
        final DictionaryType check = this.dictionaryDescRepository.findByType(dto.getType());
        if ((check != null) && (check.getId() != id)) {
            throw new UnitedException("字典类别编码不能重复", new Object[] {});
        }

        dto.setId(model.getId());
        model = this.dictionaryDescConverter.toModel(dto);
        this.dictionaryDescRepository.save(model);

        return this.dictionaryDescConverter.toDTO(model);
    }

    @Override
    public DictionaryTypeDTO details(final Long id) {

        final DictionaryType model;
        try {
            model = this.dictionaryDescRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.dictionaryDescConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        DictionaryType model;
        try {
            model = this.dictionaryDescRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        final List<Dictionary> list = this.dictionaryRepository.findByDictionaryType(model);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(k -> {
                this.dictionaryService.delete(k.getId());
            });
        }
        this.dictionaryDescRepository.deleteById(id);
    }
}
